/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.gui.inventory;

import com.kimstore.bean.Product;
import com.kimstore.factory.DaoFactory;
import com.kimstore.main.controller.InventoryTabController;
import org.jdesktop.swingx.decorator.HighlighterFactory;

/**
 *
 * @author Administrator
 */
public class ViewProductController {

    private InventoryTabController parentController;
    private ViewProductWindow window = null;
    private String productName = null;
    private Product product;

    public ViewProductController(InventoryTabController parentController, String productName) {
        this.parentController = parentController;
        this.productName = productName;
    }

    public void displayWindow() {
        product = DaoFactory.getInstance().getProductDao().findByName(productName);

        window = new ViewProductWindow(this);
        window.populateWindow(product);
        window.showWindow();
    }

    public void updateProduct(String name, float basePrice, String specification, String type) {
        product.setName(name);
        product.setBasePrice(basePrice);
        product.setSpecifications(specification);
        product.setStatus(type);

        DaoFactory.getInstance().getProductDao().saveOrUpdate(product);
        parentController.refreshTable();
    }

    void deleteProduct() {
        DaoFactory.getInstance().getProductDao().delete(product);
        parentController.refreshTable();
    }

    public void validate(String name) throws Error {
        if (name.isEmpty() || name == null) {
            throw new Error("<html><b>Product Name</b> has not been filled up</html>");
        }

        if(!name.equals(product.getName())) {
            boolean exists = DaoFactory.getInstance().getProductDao().existsName(name);

            if (exists) {
                throw new Error("<html><b>Product Name</b> has already been taken");
            }
        }
    }
}
